package com.yinkgh.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@SpringBootApplication
//读取外部配置文件
@PropertySource(value={"classpath:jdbc.properties"})
@ComponentScan(basePackages = "com.yinkgh")
//@MapperScan(value="com.yinkgh.mybatis.business.durationrecord.dao")
public class StartApplication {
	
	@Value("${jdbc.driverClassName}")
	private String jdbcDriverClassName;
    
	@Value("${jdbc.url}")
    private String jdbcUrl;
    
	@Value("${jdbc.username}")
    private String jdbcUsername;
    
	@Value("${jdbc.password}")
    private String jdbcPassword;
    
	@Value("${jdbc.initialSize}")
    private Integer initialSize;
    
    @Value("${jdbc.minIdle}")
    private Integer minIdle;
    
    @Value("${jdbc.maxActive}")
    private Integer maxActive;
    
    @Value("${jdbc.maxWait}")
    private Integer maxWait;
    
    @Value("${jdbc.timeBetweenEvictionRunsMillis}")
    private Integer timeBetweenEvictionRunsMillis;
    
    @Value("${jdbc.minEvictableIdleTimeMillis}")
    private Integer minEvictableIdleTimeMillis;
    
    @Value("${jdbc.validationQuery}")
    private String validationQuery;
    
    @Value("${jdbc.testWhileIdle}")
    private boolean testWhileIdle;
    
    @Value("${jdbc.testOnBorrow}")
    private boolean testOnBorrow;
    
    @Value("${jdbc.testOnReturn}")
    private boolean testOnReturn;
    
    @Value("${jdbc.poolPreparedStatements}")
    private boolean poolPreparedStatements;
    
    @Value("${jdbc.maxPoolPreparedStatementPerConnectionSize}")
    private Integer maxPoolPreparedStatementPerConnectionSize;
    
    @Value("${jdbc.removeAbandoned}")
    private boolean removeAbandoned;
    
    @Value("${jdbc.removeAbandonedTimeout}")
    private Integer removeAbandonedTimeout;
    
    @Value("${jdbc.logAbandoned}")
    private boolean logAbandoned;
    
    @Value("${jdbc.filters}")
    private String filters;
    
    
    @Bean(destroyMethod = "close")
	public DataSource druidDataSource() throws SQLException {
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setDriverClassName(jdbcDriverClassName);
		druidDataSource.setUrl(jdbcUrl);
		druidDataSource.setUsername(jdbcUsername);
		druidDataSource.setPassword(jdbcPassword);
		druidDataSource.setInitialSize(initialSize);
		druidDataSource.setMinIdle(minIdle);
		druidDataSource.setMaxActive(maxActive);
		druidDataSource.setMaxWait(maxWait);
		druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		druidDataSource.setValidationQuery(validationQuery);
		druidDataSource.setTestWhileIdle(testWhileIdle);
		druidDataSource.setTestOnBorrow(testOnBorrow);
		druidDataSource.setTestOnReturn(testOnReturn);
		druidDataSource.setPoolPreparedStatements(poolPreparedStatements);
		druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
		druidDataSource.setRemoveAbandoned(removeAbandoned);
		druidDataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
		druidDataSource.setLogAbandoned(logAbandoned);
		druidDataSource.setFilters(filters);
		return druidDataSource;
	}
	
	
	public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
	
	
	
	
	
	
}
